import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import { DNavBar, DAtInpute } from '@/components'
import { AtForm, AtInput, AtButton } from 'taro-ui'
import './advancefundform.scss'
import http from '@/api'
import common from '@/common'
export default class AdvancefundformBox extends Component {

    constructor(props) {
        super(props)
        this.state = {
            addform: {
                amMoney: "",
                text: ""
            },

            mqAmQuota: "",
            mqAmAvailableQuota: ""
        }
    }
    componentWillMount() {
        let { mqAmQuota, mqAmAvailableQuota } = this.$router.params
        this.setState({
            mqAmQuota,
            mqAmAvailableQuota
        })
    }
    onSubmit() {
        this.submitAppinfo()
    }
    submitAppinfo() {
        let { id } = this.$router.params
        let { amMoney, text } = this.state.addform
        if (!amMoney) {
            return Taro.showToast({
                icon: "none",
                title: "请填写申请额度"
            })
        }
        if (!text) {
            return Taro.showToast({
                icon: "none",
                title: "请填写申请原因"
            })
        }
        let obj = {
            mqQuota: amMoney,
            applyReason: text,
            refMqId: id
        }
        http.submitAppinfo(obj).then(res => {
            if (!JSON.stringify(res)) {
                return
            }
            Taro.showToast({
                title: '已代购额度申请',
                duration: 800,
                mask: true
            }).then(() => {
                setTimeout(() => {
                    Taro.redirectTo({
                        url: `/pages/financingrecord/financingrecord?id=${id}`
                    })
                }, 1000)
            })
        })
    }
    onChangeLimit(value) {
        let Digit = common.getDigit(value, this.state.addform.amMoney)
        this.setState({
            addform: {
                ...this.state.addform,
                amMoney: Digit
            }
        })
    }
    onChangeCause(value) {
        this.setState({
            addform: {
                ...this.state.addform,
                text: value
            }
        })
    }

    render() {
        const {
            addform,
            mqAmQuota,
            mqAmAvailableQuota
        } = this.state
        return (
            <View className='b_advancefundform'>
                <DNavBar title={'代购额度申请单'}></DNavBar>
                <View className='b_advancefundform__box'>
                    <View className='b_advancefundform__form'>
                        <AtForm>
                            <View className='b_advancefundform__form__item'>
                                <DAtInpute icon='none' value={mqAmAvailableQuota} type='number' title='当前可用额度' >
                                    <Text>万</Text>
                                </DAtInpute>
                                <DAtInpute icon='none' value={mqAmQuota} type='number' title='当前总额度' >
                                    <Text>万</Text>
                                </DAtInpute>

                                <DAtInpute value={addform.amMoney} type='number' onChange={this.onChangeLimit.bind(this)} title='申请提升额度' placeholder='请输入要提升的额度 '>
                                    <Text>万</Text>
                                </DAtInpute>
                                <DAtInpute value={addform.text} onChange={this.onChangeCause.bind(this)} title='申请原因' placeholder='请输入要申请原因'></DAtInpute>
                            </View>
                        </AtForm>
                    </View>
                    <View className='b_advancefundform__footer'>
                        <AtButton className='item_btn' onClick={this.onSubmit.bind(this)}>提交申请</AtButton>
                    </View>
                </View>
            </View>
        )
    }
}